﻿#region zh-CHS 2006 - 2010 DemoSoft 团队 | en 2006-2010 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the MMOSE(Massively Multiplayer Online Server Engine) for .NET.
//
//                              2006-2010 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Demo.Mmose.Core.Common.SafeCollections;
using System.Collections;
#endregion

namespace Demo.Mmose.Core.Entity.Suit.Spell
{
    /// <summary>
    /// 技能效果容器(一般添加到技能上面，分为战斗前，防御前，战斗后，防御后)
    /// </summary>
    public class EffectCollection<EffectT> : IEnumerable<EffectT>
        where EffectT : Effect
    {
        #region zh-CHS 构造和初始化和清理 | en Constructors and Initializers and Dispose

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="spells"></param>
        public EffectCollection()
        {

        }

        #endregion

        #region zh-CHS 共有属性 | en Public Properties

        #endregion

        #region zh-CHS 共有方法 | en Public Methods

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private SafeDictionary<long, EffectT> m_EffectDictionary = new SafeDictionary<long, EffectT>( 24 );
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <param name="spellId"></param>
        /// <param name="Spell"></param>
        public bool AddEffect( long effectId, EffectT effect )
        {
            return m_EffectDictionary.TryAdd( effectId, effect );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="spellId"></param>
        /// <param name="Spell"></param>
        public bool RemoveEffect( long effectId )
        {
            return m_EffectDictionary.Remove( effectId );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="spellId"></param>
        /// <returns></returns>
        public EffectT FindEffectOnId( long effectId )
        {
            EffectT effect = default( EffectT );
            m_EffectDictionary.TryGetValue( effectId, out effect );

            return effect;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="spellId"></param>
        /// <returns></returns>
        public void Clear()
        {
            m_EffectDictionary.Clear();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public EffectT[] ToArray()
        {
            return m_EffectDictionary.ToArrayValues();
        }

        #endregion

        #region zh-CHS 接口实现 | en Interface Implementation
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerator<EffectT> GetEnumerator()
        {
            EffectT[] tempEffectArray = this.ToArray();
            if ( tempEffectArray == null )
                yield break;

            for ( int iIndex = 0; iIndex < tempEffectArray.Length; iIndex++ )
                yield return tempEffectArray[iIndex];
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
        #endregion
    }
}
#endregion